package com.ruoyi.mushcoin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.core.domain.R;
import com.ruoyi.common.excel.core.ExcelResult;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.web.annotation.RepeatSubmit;
import com.ruoyi.common.web.core.BaseController;
import com.ruoyi.mushcoin.domain.bo.TgUserTaskManageBo;
import com.ruoyi.mushcoin.domain.vo.TgUserTaskManageImportVo;
import com.ruoyi.mushcoin.domain.vo.TgUserTaskManageVo;
import com.ruoyi.mushcoin.listener.TgUserTaskManageImportListener;
import com.ruoyi.mushcoin.service.ITgUserTaskManageService;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.domain.bo.SysDictDataBo;
import com.ruoyi.system.service.ISysDictDataService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户任务管理(H5)Controller
 *
 * @author billy
 * 2024-09-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mushcoin/userTaskManage")
public class TgUserTaskManageController extends BaseController {
    @Resource
    private ITgUserTaskManageService tgUserTaskManageService;
    @Resource
    private ISysDictDataService dictDataService;

    /**
     * 查询用户任务管理(H5)列表
     */
    @SaCheckPermission("mushcoin:userTaskManage:list")
    @GetMapping("/list")
    public TableDataInfo<TgUserTaskManageVo> list(TgUserTaskManageBo tgUserTaskManageBo) {
        return tgUserTaskManageService.selectPage(tgUserTaskManageBo);
    }

    /**
     * 导出用户任务管理(H5)列表
     */
    @SaCheckPermission("mushcoin:userTaskManage:export")
    @Log(title = "用户任务管理(H5)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TgUserTaskManageBo tgUserTaskManageBo) {
        List<TgUserTaskManageVo> list = tgUserTaskManageService.selectList(tgUserTaskManageBo);
        ExcelUtil.exportExcel(list, "用户任务管理(H5)", TgUserTaskManageVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "用户任务管理(H5)", businessType = BusinessType.IMPORT)
    @SaCheckPermission("mushcoin:userTaskManage:import")
    @PostMapping("/importData")
    public R<Void> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<TgUserTaskManageImportVo> result = ExcelUtil.importExcel(file.getInputStream(), TgUserTaskManageImportVo.class, new TgUserTaskManageImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    @SaCheckPermission("mushcoin:userTaskManage:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "用户任务管理(H5)", TgUserTaskManageImportVo.class, response);
    }

    /**
     * 获取用户任务管理(H5)详细信息
     */
    @SaCheckPermission("mushcoin:userTaskManage:query")
    @GetMapping(value = "/{id}")
    public R<TgUserTaskManageVo> getInfo(@PathVariable Long id) {
        return R.ok(tgUserTaskManageService.selectById(id));
    }

    /**
     * 新增用户任务管理(H5)
     */
    @SaCheckPermission("mushcoin:userTaskManage:add")
    @Log(title = "用户任务管理(H5)", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public R<Void> add(@Validated @RequestBody TgUserTaskManageBo tgUserTaskManageBo) {
        Date now = new Date();
        boolean inserted = tgUserTaskManageService.insert(tgUserTaskManageBo);
        if (!inserted) {
            return R.fail("新增用户任务管理(H5)记录失败！");
        }else{
            //添加基础数据类型： dict_type="tg_rewards_task_type"
            SysDictDataBo dictDataBo = new SysDictDataBo();
            dictDataBo.setDictType("tg_rewards_task_type");
            dictDataBo.setTenantId(1L);
            dictDataBo.setDictLabel(tgUserTaskManageBo.getTaskName());
            dictDataBo.setDictValue(tgUserTaskManageBo.getId().toString());
            dictDataBo.setDictSort(tgUserTaskManageBo.getId().intValue());
            dictDataBo.setCreateTime(now);
            dictDataBo.setUpdateTime(now);
            dictDataBo.setCreateBy(1L);
            dictDataBo.setUpdateBy(1L);
            dictDataService.insertDictData(dictDataBo);
        }
        return R.ok();
    }

    /**
     * 修改用户任务管理(H5)
     */
    @SaCheckPermission("mushcoin:userTaskManage:edit")
    @Log(title = "用户任务管理(H5)", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public R<Void> edit(@Validated @RequestBody TgUserTaskManageBo tgUserTaskManageBo) {
        Date now = new Date();
        Boolean updated = tgUserTaskManageService.update(tgUserTaskManageBo);
        if (!updated) {
            return R.fail("修改用户任务管理(H5)记录失败!");
        }else{
            //添加基础数据类型： dict_type="tg_rewards_task_type"
            SysDictData dictData = new SysDictData();
            dictData.setDictType("tg_rewards_task_type");
            dictData.setDictValue(tgUserTaskManageBo.getId().toString());
            dictData.setDictLabel(tgUserTaskManageBo.getTaskName());
            dictData.setUpdateTime(now);

            dictDataService.updateDictByDictTypeAndValue(dictData);
        }
        return R.ok();
    }

    /**
     * 删除用户任务管理(H5)
     */
    @SaCheckPermission("mushcoin:userTaskManage:remove")
    @Log(title = "用户任务管理(H5)", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        boolean deleted = tgUserTaskManageService.deleteByIds(ids);
        if (!deleted) {
            return R.fail("删除用户任务管理(H5)记录失败!");
        }
        return R.ok();
    }
}
